package com.hmh.student.students.controller;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.students.entity.Course;
import com.hmh.student.students.entity.Student;
import com.hmh.student.students.entity.StudentCourse;
import com.hmh.student.students.service.ICourseService;
import com.hmh.student.students.service.IStudentCourseService;
import com.hmh.student.students.service.IStudentService;
import com.hmh.student.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    IStudentService studentService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IStudentCourseService studentCourseService;
//修改
    @PutMapping("/updateStudent")
    public Map<String, Object> updateStudent(@RequestBody Student student) {
        Map<String, Object> map = new HashMap<>();
        try {
            studentService.updateById(student);
            map.put("msg","success");
        } catch (Exception e) {
            map.put("msg","注册失败"+e.getMessage());

        }
        return map;
    }



    //添加
    @PostMapping("/addStudent")
    public Map<String,Object> addStudent(@RequestBody Student student) {
        Map<String, Object> map = new HashMap<>();
        try {
           studentService.save(student);
            map.put("msg", "success");
            map.put("studentNo",student.getStudentNo());
        } catch (Exception e) {
            map.put("msg", "fail");
            map.put("msg",e.getMessage());
        }
        return map;
    }

    //删除
    @DeleteMapping("/deleteStudent")
    public Map<String,Object> deleteStudent(@RequestParam Integer studentNo) {
        Map<String, Object> map = new HashMap<>();
        try {
            studentService.removeById(studentNo);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
        }
        return map;
    }



    //分页查询
    @GetMapping("/pageStudent")
    public Page<Student> pageStudent(@RequestParam(required = false) String name,
                                   @RequestParam(required = false) String gender,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     @RequestParam(defaultValue = "1") Integer current) {

        return studentService.queryStudentPage(name,gender,pageSize,current);
    }

//查询所有的课程
    @GetMapping("/findAllCourse")
    public Result findAllCourse(){
        List<Course> list = courseService.list();
        return Result.success(list);
    }

  //获取学生当前课程
  @GetMapping("/getCourseByStudentId")
  public Result getCourseByStudentId(@RequestParam Integer studentNo) {
        if (studentNo == null || studentNo.equals("")) {
            return  Result.fail("学号不能为空");
        }
        List<Integer> courseId = studentCourseService.getCourseByStudentId(studentNo);
      if (courseId == null || courseId.isEmpty()) {
          return Result.success("学生还未选择课程", Collections.emptyList());
      }

      return Result.success(courseId);
  }
  //分配课程
@PutMapping("/assignCourses")
  public Result assignCourses(
          @RequestParam Integer studentNo,
          @RequestParam List<Integer> courseNo) {
        if (studentNo == null || courseNo ==null || studentNo.equals("") || courseNo.equals("")) {
            return Result.success("学号和课程号不能为空");
        }
        try{
            boolean result = studentCourseService.assignCourses(studentNo, courseNo);
            return result ? Result.success("课程分配成功")
                    : Result.fail("课程分配失败");
        }catch (Exception e){
            return Result.fail("分配角色失败"+e.getMessage());
        }
  }

}
